package com.migo.service;

import com.migo.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/9 0009.
 */
@Service
public class PicService {
    @Value("${IMAGE_SERVER_BASE_URL}")
    private String IMAGE_SERVER_BASE_URL;

    public Map uploadFile(MultipartFile uploadFile){
        try{
//            System.out.println("IMAGE_SERVER_BASE_URL:" + IMAGE_SERVER_BASE_URL);
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:properties/client.properties");
            String filename = uploadFile.getOriginalFilename();
            System.out.println("OriginalFilename:" + filename);
            String extendName = filename.substring(filename.lastIndexOf(".")+1);
            String url = fastDFSClient.uploadFile(uploadFile.getBytes(),extendName);
            url = IMAGE_SERVER_BASE_URL + url;
            Map result = new HashMap();
            result.put("error",0);
            result.put("url",url);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Map result = new HashMap();
            result.put("error",1);
            result.put("message","上传失败");
            return result;
        }
    }

}
